package com.mstore.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.mstore.pojo.Account;

public class AccountDao extends SuperDao {

	private static Logger logger = Logger.getLogger(AccountDao.class.getName());

	public void save(Account account) {
		super.save(account);
	}

	public void update(Account account) {
		super.update(account);
	}

	public Account findAccount(Account account) {
		String hql = "from Account where mobile=? and password=?";
		List<Object> objects = super.getObject(hql,
				new String[] { account.getMobile(), account.getPassword() });
		if (objects.size() > 0) {
			return (Account) objects.get(0);
		} else {
			return null;
		}
	}

	/**
	 * 验证企业标示符的唯一性
	 */
	public boolean checkParamKey(String paramKey) {
		String hql = "from Account where paramKey=?";
		try {
			List<Object> objects = super.getObject(hql,
					new String[] { paramKey });
			return (objects.size() > 0 ? true : false);
		} catch (Exception e) {
			return false;
		}
	}

	/**
	 * 通过标示符查找企业ID
	 */
	public Object selectIdByParamKey(String paramKey) {
		String hql = "from Account where paramKey=?";
		List<Object> objects = super.getObject(hql, new Object[] { paramKey });
		if (objects.size() > 0) {
			return objects.get(0);
		} else {
			return null;
		}

	}

	/**
	 * 验证企业名称的唯一性
	 */
	public boolean checkActName(String actName) {
		String hql = "from Account where name=?";
		List<Object> objects = super.getObject(hql, new Object[] { actName });
		return objects.size() > 0;
	}

	/**
	 * 验证企业名称但排除自己
	 */
	public boolean checkActName1(String actName, Long id) {
		String hql = "from Account where name=? and id != ?";
		List<Object> objects = super.getObject(hql,
				new Object[] { actName, id });
		return objects.size() > 0;
	}

	/**
	 * 验证企业邮箱的唯一性
	 */
	public boolean checkActEmail(String email) {
		String hql = "from Account where email=?";
		List<Object> objects = super.getObject(hql, new Object[] { email });
		return objects.size() > 0;
	}

	/**
	 * 验证企业手机号的唯一性
	 */
	public boolean checkActMobile(String mobile) {
		String hql = "from Account where mobile=?";
		List<Object> objects = super.getObject(hql, new Object[] { mobile });
		return objects.size() > 0;
	}

	/**
	 * 修改密码
	 */
	public void updatePassword(Account account) {
		StringBuilder sql = new StringBuilder(
				"update b_account set password= '");
		sql.append(account.getPassword());
		sql.append("' where id=");
		sql.append(account.getId());
		super.executeUpdate(sql.toString());
	}

	/**
	 * 当输入密码为空时
	 */
	public void updateAccountPassword(Account account) {
		// String sql = "update b_account  set  name='" + account.getName()
		// + "', email= '" + account.getEmail() + "', loginName='"
		// + account.getLoginName() + "', paramkey='"
		// + account.getParamKey() + "', mobile='" + account.getMobile()
		// + "' where  id=" + account.getId();

		String separate = " set ";
		StringBuilder sb = new StringBuilder();
		sb.append("update b_account");
		String name = account.getName();
		if (StringUtils.isNotBlank(name)) {
			sb.append(separate);
			sb.append("name = '");
			sb.append(name);
			sb.append("'");
			separate = ", ";
		}

		String email = account.getEmail();
		if (StringUtils.isNotBlank(email)) {
			sb.append(separate);
			sb.append("email = '");
			sb.append(email);
			sb.append("'");
			separate = ", ";
		}

		String loginName = account.getLoginName();
		if (StringUtils.isNotBlank(loginName)) {
			sb.append(separate);
			sb.append("loginName = '");
			sb.append(loginName);
			sb.append("'");
			separate = ", ";
		}

		String paramkey = account.getParamKey();
		if (StringUtils.isNotBlank(paramkey)) {
			sb.append(separate);
			sb.append("paramkey = '");
			sb.append(paramkey);
			sb.append("'");
			separate = ", ";
		}

		String mobile = account.getMobile();
		if (StringUtils.isNotBlank(mobile)) {
			sb.append(separate);
			sb.append("mobile = '");
			sb.append(mobile);
			sb.append("'");
		}

		sb.append("where id = ");
		sb.append(account.getId());

		super.executeUpdate(sb.toString());
	}

	/**
	 * 修改企业信息
	 */
	public void updateAccountMessage(Account account) {
		// String sql = " update b_account  set  name='" + account.getName()
		// + "', email= '" + account.getEmail() + "', loginName='"
		// + account.getLoginName() + "', paramkey='"
		// + account.getParamKey() + "', mobile='" + account.getMobile()
		// + "' where  id=" + account.getId();
		String separate = " set ";
		StringBuilder sb = new StringBuilder();
		sb.append("update b_account");
		String name = account.getName();
		if (StringUtils.isNotBlank(name)) {
			sb.append(separate);
			sb.append("name = '");
			sb.append(name);
			sb.append("'");
			separate = ", ";
		}

		String email = account.getEmail();
		if (StringUtils.isNotBlank(email)) {
			sb.append(separate);
			sb.append("email = '");
			sb.append(email);
			sb.append("'");
			separate = ", ";
		}

		String loginName = account.getLoginName();
		if (StringUtils.isNotBlank(loginName)) {
			sb.append(separate);
			sb.append("loginName = '");
			sb.append(loginName);
			sb.append("'");
			separate = ", ";
		}

		String paramkey = account.getParamKey();
		if (StringUtils.isNotBlank(paramkey)) {
			sb.append(separate);
			sb.append("paramkey = '");
			sb.append(paramkey);
			sb.append("'");
			separate = ", ";
		}

		String mobile = account.getMobile();
		if (StringUtils.isNotBlank(mobile)) {
			sb.append(separate);
			sb.append("mobile = '");
			sb.append(mobile);
			sb.append("'");
		}

		sb.append("where id = ");
		sb.append(account.getId());

		super.executeUpdate(sb.toString());
	}

	/**
	 * 根据id查找
	 */
	public Account findAccountById(Account account) {
		String hql = "from Account where id = ?";
		List<Object> objects = super.getObject(hql,
				new Object[] { account.getId() });
		if (objects.size() > 0) {
			return (Account) objects.get(0);
		} else {
			return null;
		}
	}
	
	public Account FindAccountById(Account account,long id){
		Account a=(Account) this.getHibernateTemplate().get(account.getClass(), id);
		return a;
	}

	public void UpdatePassword(Account account){
		this.getHibernateTemplate().update(account);
	}
	
	/**
	 * 根据member_id查找
	 */
	public Object findAccountByMemberId(Account account) {
		/*
		 * String hql = "from Account a where a.member.id = " +
		 * account.getMember().getId();
		 */
		StringBuilder sb = new StringBuilder();
		sb.append("from Account a where a.member.id = ");
		sb.append(account.getMember().getId());

		List<Object> objects = super.find(sb.toString());
		if (objects != null && objects.size() > 0) {
			return objects.get(0);
		} else {
			return null;
		}
	}

	/**
	 * 查询全部商户
	 */
	public List<Object> tmpGetAll() {
		String hql = "from Account";
		return super.find(hql);
	}

	/**
	 * 临时
	 */
	public void tmpUpdate(Account account) {
		/*String sql = "update b_account set member_id = "
				+ account.getMember().getId() + " where id = "
				+ account.getId();
		super.executeUpdate(sql);*/
		StringBuilder sb=new StringBuilder();
		sb.append("update b_account set member_id = ");
		sb.append(account.getMember().getId());
		sb.append("where id =");
		sb.append(account.getId());
		super.executeUpdate(sb.toString());
	}

	/**
	 * 对比手机号码查询
	 * 
	 * @return
	 */
	public Object getByMobile(Account account) {

		/*
		 * String sql =
		 * "SELECT b.member_id  FROM  b_account b JOIN   c_member c ON b.member_id=c.id WHERE b.id="
		 * + account.getId();
		 */
		StringBuilder sb = new StringBuilder();
		sb.append("SELECT b.member_id  FROM  b_account b JOIN   c_member c ON b.member_id=c.id WHERE b.id=");
		sb.append(account.getId());
		return super.executeSql(sb.toString());
	}

	/**
	 * 通过email修改密码
	 * 
	 * @return
	 * 
	 */
	public void updatePasswordByEmail(Account account) {
		// String
		// sql="update b_account   set  password='"+account.getPassword()+"' where  email='"+account.getEmail()+"'";
		StringBuilder sb = new StringBuilder();
		sb.append("update b_account set password = '");
		sb.append(account.getPassword());
		sb.append("' where email = '");
		sb.append(account.getEmail());
		sb.append("'");
		super.executeUpdate(sb.toString());
	}

	/**
	 * 通过memberId修改密码
	 * 
	 * @return
	 */
	public void updatePasswordByMobile(Account account) {
		// String sql
		// ="update b_account   set  password='"+account.getPassword()+"'   where  member_id="+account.getMember().getId();
		StringBuilder sb = new StringBuilder();
		sb.append("update b_account set password = '");
		sb.append(account.getPassword());
		sb.append("' where member_id = '");
		sb.append(account.getMember().getId());
		sb.append("'");
		super.executeUpdate(sb.toString());
	}

	/**
	 * 分页查询全部企业
	 */
	public List<Object> findAllAccountByPage(int index) {
		String hql = "from Account";
		return super.findPage(index, hql);
	}

	/**
	 * 查询企业总数
	 */
	public int getAccountCount() {
		String sql = "SELECT COUNT(ac) FROM Account ac";
		List<Object> objects = super.find(sql);
		if (objects.size() > 0) {
			Long count = (Long) objects.get(0);
			return count.intValue();
		}
		return 0;
	}

	/**
	 * 查询商户实时余额总数
	 */
	public float getSumRbalance() {
		String sql = "SELECT SUM(rbalance) FROM Account";
		List<Object> objects = super.find(sql);
		if (objects.size() > 0) {
			Double count = (Double) objects.get(0);
			return count.floatValue();
		}
		return 0f;
	}

	/**
	 * 通过企业名称、手机号模糊查询该企业
	 */
	public List<Object> getAccoutsBySearch(String searchText) {
		try {
			StringBuilder sb = new StringBuilder();
			sb.append("from Account where name like '%");
			sb.append(searchText);
			sb.append("%' or mobile like '%");
			sb.append(searchText);
			sb.append("%'");
			return find(sb.toString());
		} catch (Exception e) {
			logger.error("模糊查询企业报错", e);
		}
		return new ArrayList<Object>();
	}
	
	/**
	 * 查询实时余额大于0的商户
	 */
	public List<Object> getAccountsHasRbalance() {
		String sql = "FROM Account WHERE rbalance > 0";
		return find(sql);
	}
	
	/**
	 * 根据月份查询商户数量
	 */
	public int getAccountsInMonth(String beginMonth, String endMonth) {
		String sql = "select count(ac) from Account ac where ac.createTime between ? AND ?";
		List<Object> objects = getObject(sql, new Object[] { beginMonth, endMonth });
		if (objects.isEmpty()) {
			return 0;
		}
		return ((Long)objects.get(0)).intValue();
	}

	/**
	 * 通过企业名称查询企业
	 */
	public List<Object> searchAccountNameAuto(String searchText) {
		try {
			StringBuilder sb = new StringBuilder();
			sb.append("from Account where name like '%");
			sb.append(searchText);
			sb.append("%'");
			return findByLimit(0, 10, sb.toString());
		} catch (Exception e) {
			logger.error("模糊查询企业报错", e);
		}
		return new ArrayList<Object>();
	}
	
}
